import { createVitePlugins } from './plugin'
import { resolve } from 'path'
import { ConfigEnv, loadEnv, UserConfig } from 'vite'
import { wrapperEnv } from './utils'

const pathResolve = (dir: string) => {
  return resolve(process.cwd(), '.', dir)
}
export default function ({ command, mode }: ConfigEnv): UserConfig {
  const isProduction = command === 'build'
  const root = process.cwd()
  const env = loadEnv(mode, root)
  const viteEnv = wrapperEnv(env)

  return {
    root,
    resolve: {
      alias: [
        {
          find: 'vue-i18n',
          replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
        },
        // @/xxxx => src/xxxx
        {
          find: /@\//,
          replacement: pathResolve('src') + '/',
        },
        // #/xxxx => types/xxxx
        {
          find: /#\//,
          replacement: pathResolve('types') + '/',
        },
      ],
    },
    plugins: createVitePlugins(viteEnv, isProduction),
    build: {
      minify: 'terser',
      terserOptions: {
        compress: {
          //生产环境时移除console
          drop_console: true,
          drop_debugger: true,
        },
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: `true; @import (reference) "${resolve('src/styles/css/mixin.less')}";`,
          },
          javascriptEnabled: true,
        },
      },
    },
  }
}
